<template>
  <div class="saveUpdate">
    <el-dialog :close-on-click-modal="false" :title="id?'编辑快捷回复':'新增快捷回复'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="500px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="120px" size="mini">
        <dialog-wrapper :MaxWidth="400">
          <el-form-item label="标题：" prop="title" :rules="[{ required: true, message: '标题不能为空'}]">
            <el-input v-model="DetailInfo.title" placeholder="填写标题..."
            ></el-input>
          </el-form-item>
          <el-form-item label="内容：" prop="content"
                        :rules="[{ required: true, message: '内容不能为空'}]">
            <el-input v-model="DetailInfo.content" placeholder="填写内容..." type="textarea" :rows="4"></el-input>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定
        </el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import {addQuickReply, updateQuickReply} from '@/api'

  export default {
    components: {DialogWrapper},
    data() {
      return {
        setbank: false,
        showDialog: this.showUpdate,
        loading: false,
        DetailInfo: this.Detail
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      id: {
        type: Number
      },
      Detail: {
        type: Object
      }
    },
    // created() {
    //   if (!this.id) {
    //     this.DetailInfo={}
    //   }
    // },
    methods: {
      saveDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            this.loading = true
            addQuickReply(this.DetailInfo).then(res => {
              if (res.status === 0) {
                this.loading = false
                this.$message({message: '添加成功。', type: 'success'})
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      updateDetail() {
        this.loading = true
        updateQuickReply(this.id, this.DetailInfo).then(res => {
          if (res.status === 0) {
            this.loading = false
            this.$message({message: '编辑成功', type: 'success'})
            this.$emit('update:showUpdate', false)
          }
        })
      }

    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>
